function toArray(p_array){    
    var f_array=new Array();
    if(!p_array)return f_array;
    (isNaN(p_array.length))?f_array[0]=P_array:f_array=p_array;
    return f_array;
}
function showul(ob){
    f=(document.getElementById('_'+ob.id).className=='vi')
    ob.className=((f)?'ch':'che')
    document.getElementById('_'+ob.id).className=((f)?'hi':'vi')
}


function CheckeableTree(form){
    checks=toArray(form.itAcciones);
    T=checks.length
    //constructor
    addEvents();
    var MAP=createMap();
    function addEvents(){
        for(var i=0;i<T;i++){
            checks[i].onclick=function(){
                checkChilds(this);
                if(this.checked){
                    checkParents(this)
                }else{
                    disCheckedParent(this);
                }
            }
        }
    }
    function checkChilds(ob){
        for(var i=0;i<T;i++){
            if(checks[i].getAttribute('parent')==ob.value && ob.value != checks[i].value){
                checks[i].checked=ob.checked;
                checkChilds(checks[i]);
            }
        }
    }
    function checkParents(ob){
        for(var i=0;i<T;i++){
            if(checks[i].value==ob.getAttribute('parent') && ob.value != checks[i].value){
                checks[i].checked=true;
                checkParents(checks[i]);
                break;
            }
        }
    }
    function disCheckedParent(ob){
        var f=true;
        var abue=-1;
        for(var i=0;i<T;i++){
            if(checks[i].value==ob.getAttribute('parent') && ob.value != checks[i].value){
                abue=i;
                for(var j=0;j<T;j++){
                    if(checks[j].getAttribute('parent')==checks[i].value && i!=j && checks[j].value != ob.value && checks[j].checked==true){
                        f=false;
                        break;
                    }
                }

                break;
            }
        }
        if(f){
            if(abue!=-1){
                checks[abue].checked=false;
                disCheckedParent(checks[abue]);
            }
        }
    }

    this.change=function(Obj){
        if(Obj.className=='expand'){
            Obj.className='contract';
            Obj.title='Contraer el Árbol'
            this.expand();
        }else{
            Obj.className='expand';
            Obj.title='Expandir el Árbol'
            this.contract();
        }
    }
    this.expand=function(){
        changeClassChild('hi','vi');
    }
    this.contract=function(){
        changeClassChild('vi','hi');
    }
    this.check=function(Obj){
        if(Obj.className=='check_all'){
            Obj.className='no_check';
            Obj.title='Seleccionar Ninguno'
            toCheck(true);
        }else{
            Obj.className='check_all';
            Obj.title='Seleccionar Todos'
            toCheck(false);
        }
    }
    function toCheck(flag){
        for(var i=0;i<T;i++){
            checks[i].checked=flag;
        }
    }
    function changeClassChild(c1,c2){
        for(var i=0;i<T;i++){
            if(document.getElementById('_'+checks[i].value)){
                if(document.getElementById('_'+checks[i].value).className==c1){
                    document.getElementById('_'+checks[i].value).className=c2;
                }
            }
        }
    }
    this.toSelect=function(key){
        if(MAP[key]>=0){
            checks[MAP[key]].checked=true;
        }
    }
    function createMap(){
        map=new Object()
        for(var i=0;i<T;i++){
            var key=checks[i].value;
            map[key]=i;
        }
        return map;
    }
}
